#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"parcours.c"
#include"capteur.c"
#include"personnel.c"
#include"patient.c"
#include"menu_principal.h"



int main()
{
int choix;
authentification();

}

void authentification()
{
int k,id,t,idt,choix;
char chemin1[]="mots_de_passe1.txt";
char chemin2[]="mots_de_passe2.txt";
char mdp[50];
char psw[50];
FILE *f1,*f2;



printf("\n1:acces directeur\n2:acces securité\n");
scanf("%d",&k);
switch (k)
{
  case 1 :
   f1=fopen(chemin1,"r");
if(f1==NULL)printf("\nimpo\n");
else{
   printf("\nid:\n");
   scanf("%d",&id);
   printf("\nmdp:\n");
   scanf("%s",mdp);
   t=0;
   while(fscanf(f1,"%d %s",&idt,psw)!=EOF){
   if(id==idt && strcmp(mdp,psw)==0)
   {t=1;break;}}
   if(t==1)
   {printf("\n1:menu personnels\n2:menu patients\n3:changer mot de passe\nautre pour quitter\n");
   scanf("%d",&choix);
   switch(choix){
    case 1 :
    menu_personnel();
    break;
    case 2 :
    menu_patient();
    break;
case 3 :
change_psw(id,mdp,chemin1);
break;
    default : main();
    break;}} 
else printf("\nerreur\n");
main();
break;
}
   case 2 :
   f2=fopen(chemin2,"r");
if(f2==NULL)printf("\nimpo\n");
else{
    printf("\nid:\n");
    scanf("%d",&id);
    printf("\nmdp:\n");
    scanf("%s",mdp);
    t=0;
    while(fscanf(f2,"%d %s",&idt,psw)!=EOF){
    if(id==idt && strcmp(mdp,psw)==0)
    {t=1;break;}}
    if(t==1)
    {printf("\n1:menu capteur \n2:menu parcours\n3:changer mot de passe\nautre pour quitter\n");
    scanf("%d",&choix);
    switch(choix){
     case 1 :
     menu_capteur();
     break;
     case 2 :
     menu_parcours();
     break;
case 3 :
change_psw(id,mdp,chemin2);
break;
     default :main();
     break;}}


else printf("\nerreur\n%d",t);
main();
break;

 default : break;
}}
}

void change_psw(int id,char mdp [],char chemin[])
{
int idt;
char nmdp[50],cmdp[50],psw[50];
char temp[]="temp.txt";
FILE *f1,*f2;
f1=fopen(chemin,"r");
f2=fopen(temp,"w");
if(f1==NULL||f2==NULL) printf("\nimpossible\n");
else{
printf ("\ndonner le nouveau mot de passe \n");
scanf("%s",nmdp);
printf("\nconfirmer le nouveau mot de passe\n");
scanf("%s",cmdp);
if(strcmp(nmdp,cmdp)==0){
strcpy(mdp,nmdp);
while (fscanf(f1,"%d %s",&idt,psw)!=EOF){
if(id!=idt)
fprintf(f2,"%d %s\n",idt,psw);}
fprintf(f2,"%d %s\n",id,mdp);
fclose(f1);
fclose(f2);
remove(chemin);
rename(temp,chemin);}
printf("\ndone!");
}}
